﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using laboration2.Models.Abstract;
using laboration2.Models.Repositories;
using laboration2.Models;

namespace laboration2.Controllers
{
    public class ListController : Controller
    {

        // Konstanter
        const int pageSize = 20;
        const string DELETE_ERROR = "Ett fel inträffade då en kontakt skulle tas bort, försök igen eller kontakta administratören";
        const string UPDATE_ERROR = "Ett fel inträffade då en kontakt skulle ändras bort, försök igen eller kontakta administratören";
        const string ADD_ERROR = "Ett fel inträffade då en kontakt skulle läggas till, försök igen eller kontakta administratören";

        private IRepository _repository;

        public ListController()
            : this(new EFRepository())
        {

        }

        public ListController(IRepository repository)
        {
            this._repository = repository;
        }

        public ActionResult Index(int? page)
        {
            var model = this._repository.GetContacts((page ?? 0)*pageSize, pageSize);

            ViewBag.HasPrevious = model.HasPrevious;
            ViewBag.HasNext = model.HasNext;
            ViewBag.CurrentPage = (page ?? 0);
            return View("Index", model.Enteties);
        }

        /**
         * Create (GET)
         */
        public ActionResult Create()
        {
            return View("Create");
        }

        /**
        * Create (Post)
        */
        [HttpPost]
        public ActionResult Create(Contact contact)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    this._repository.AddContact(contact);
                    this._repository.Save();
                }
                catch (Exception)
                {
                    ModelState.AddModelError(String.Empty, ADD_ERROR);
                }
                return RedirectToAction("Index");
                //return View("Success", contact);
            }
            return View("Create", contact);
        }

        /**
    * Edit (GET)
    */
        public ActionResult Edit(int id)
        {
            var model = this._repository.GetContactById(id);
            return (model == null)? View("NotFound") : View("Edit", model);
        }

        /**
        * Edit (Post)
        * @TODO: NOT FOUND PAGE 
        */
        [HttpPost]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            var model = this._repository.GetContactById(id);
            if (model == null) 
            {
                return View("NotFound");
            }

            if (TryUpdateModel(model, new string[] { }))
            {
                if (TryUpdateModel(model, new[] { "FirstName, LastName, EmailAdress" }, formValues))
                {
                    try
                    {
                        this._repository.Save();
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError(String.Empty, UPDATE_ERROR);
                    }
                }
            }
            return View("Index", model);
            //return View("Success", contact);
        }

        /**
        * Delete (GET)
        */
        public ActionResult Delete(int id)
        {
            var model = this._repository.GetContactById(id);
            return (model == null) ? View("Index", model) : View("Delete", model);
        }

        /**
        * Delete (POST)
        */
        [ActionName("Delete")]
        [HttpPost]
        public ActionResult Delete_POST(int id)
        {
            var model = this._repository.GetContactById(id);
            if (model == null)
            {
                return View("NotFound");
            }
            if (model.ContactID <= 19977)
            {
                ModelState.AddModelError(String.Empty, DELETE_ERROR);
                return RedirectToAction("Index");
            }

            try
            {
                this._repository.DeleteContact(model);
                this._repository.Save();
            }
            catch (Exception)
            {
                ModelState.AddModelError(String.Empty, DELETE_ERROR);
            }
            return (model == null) ? View("Index", model) : View("Delete", model);
        }


        /**
        * Details (GET)
        */
        public ActionResult Details(int id)
        {
            var model = this._repository.GetContactById(id);
            return (model == null) ? View("Index", model) : View("Details", model);
        }

    }
}
